package com.leapp.sk.viewdelegate.login

import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.ListView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestManager
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.engine.DiskCacheStrategy.*
import com.bumptech.glide.request.RequestOptions
import com.example.baselibrary.util.LKToastUtil
import com.example.mvp.bean.BottomChoseEntity
import com.example.mvp.delegate.BaseAppDelegate
import com.example.mvp.view.*
import com.leapp.sk.R
import com.leapp.sk.ui.activity.login.MobileVerificationActivity
import com.leapp.sk.ui.activity.login.PerfectUserInfoActivity

/**
 * 完善信息视图托管层
 * @author  chaohaipeng  on 2018/7/11.
 */
class PerfectUserInfoDelegate : BaseAppDelegate() {
    /**
     * 图片加载管理
     */
    var mRequestManager: RequestManager? = null

    /**
     * 图片加载操作
     */
    var mRequestOptions: RequestOptions = RequestOptions()


    /**
     * 底部弹出Dialog
     */
    private var windowBottomDialog: WindowBottomDialog? = null
    /**
     * 底部弹出View
     */
    private var bottomView: View? = null

    /**
     * 第一个选项文本
     */
    private var tv_dailog_bottom_frist_text: TextView? = null
    /**
     * 第二个选项文本
     */
    private var tv_dailog_bottom_second_text: TextView? = null
    /**
     * 取消选项文本
     */
    private var tv_dailog_bottom_cancel_text: TextView? = null

    /**
     * 标题
     */
    private var tv_dailog_bottom_title_text: TextView? = null
    /**
     * 底部弹出框
     */
    private var bottomChoseDialog:BottomChoseDialog?=null
    override fun initWidget() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.activity_perfect_user_info
    }

    /**
     * 初始化底部弹出
     */
    fun initBottomDialog() {
        if (windowBottomDialog == null) {
            windowBottomDialog = WindowBottomDialog(getActivity(), 0)
        }
    }

    /**
     * 初始化底部弹出视图
     */
    fun initBottomDialogView() {
        if (bottomView == null) {
            bottomView = View.inflate(getActivity(), R.layout.layout_bottom_dialog, null)
            tv_dailog_bottom_frist_text = bottomView?.findViewById<TextView>(R.id.tv_dailog_bottom_frist_text)
            tv_dailog_bottom_second_text = bottomView?.findViewById<TextView>(R.id.tv_dailog_bottom_second_text)
            tv_dailog_bottom_cancel_text = bottomView?.findViewById<TextView>(R.id.tv_dailog_bottom_cancel_text)
            tv_dailog_bottom_title_text = bottomView?.findViewById<TextView>(R.id.tv_dailog_bottom_title_text)

        }
    }



    /**
     * 设置头像
     */
    fun setHeadData(path: String?) {
        val iv_apui_head = getView<ImageView>(R.id.iv_apui_head)
        mRequestManager = Glide.with(getActivity<AppCompatActivity>())
        mRequestOptions.diskCacheStrategy(NONE)
        mRequestOptions.skipMemoryCache(true)
        mRequestOptions.error(R.mipmap.icon_defout_head)
        mRequestOptions.transforms(GlideCircleTransform(getActivity()))
        mRequestManager?.load(path)
                ?.thumbnail(0.2f)
                ?.apply(mRequestOptions)
                ?.into(iv_apui_head)
    }

    /**
     * 初始化性别底部弹出窗
     */
    fun initSexDialog() {
        initBottomDialog()
        initBottomDialogView()
        tv_dailog_bottom_frist_text?.text = getActivity<AppCompatActivity>().resources?.getString(R.string.string_info_dialog_male)
        tv_dailog_bottom_second_text?.text = getActivity<AppCompatActivity>().resources?.getString(R.string.string_info_dialog_female)
        tv_dailog_bottom_title_text?.text = getActivity<AppCompatActivity>().resources?.getString(R.string.string_info_dialog_sex_title)
        windowBottomDialog?.setBottomView(bottomView)
        windowBottomDialog?.show()
        initEvent(true)
    }

    /**
     * 初始化亲子关系底部弹出窗
     */
    fun initRelationshipDialog() {
         if (bottomChoseDialog==null){
              bottomChoseDialog = BottomChoseDialog(getActivity(), 0)
         }
        bottomChoseDialog?.setTitle(getActivity<AppCompatActivity>().resources?.getString(R.string.string_info_dialog_relatonshape_title))
        val mutableListOf = mutableListOf<BottomChoseEntity>()
        val bottomChoseEntitygp = BottomChoseEntity()
        bottomChoseEntitygp.name=getActivity<AppCompatActivity>().resources?.getString(R.string.string_info_dialog_grandpa)
        val bottomChoseEntitygm = BottomChoseEntity()
        bottomChoseEntitygm.name=getActivity<AppCompatActivity>().resources?.getString(R.string.string_info_dialog_grandma)
        val bottomChoseEntityf = BottomChoseEntity()
        bottomChoseEntityf.name=getActivity<AppCompatActivity>().resources?.getString(R.string.string_info_dialog_father)
        val bottomChoseEntitm = BottomChoseEntity()
        bottomChoseEntitm.name=getActivity<AppCompatActivity>().resources?.getString(R.string.string_info_dialog_mother)
        mutableListOf.add(bottomChoseEntitygp)
        mutableListOf.add(bottomChoseEntitygm)
        mutableListOf.add(bottomChoseEntityf)
        mutableListOf.add(bottomChoseEntitm)
        bottomChoseDialog?.setData(mutableListOf)
        bottomChoseDialog?.setEventInterface(object :BottomChoseDialog.DialogEventInterface{
            override fun OnItemClick(position: Int) {
                val get = mutableListOf.get(position)
                setRelationshipData(get.name)
            }
        })
    }

    /**
     * 弹起Dialog
     */
    fun showRelationshipDialog(){
        bottomChoseDialog?.show()
    }


    /**
     * 初始化事件
     */
    fun initEvent(isSex: Boolean) {
        initBottomDialog()
        initBottomDialogView()
        tv_dailog_bottom_frist_text?.setOnClickListener(View.OnClickListener {
            windowBottomDialog?.dismiss()
            if (isSex) {
                setSexData(getActivity<AppCompatActivity>().resources?.getString(R.string.string_info_dialog_male))
            } else {
                setRelationshipData(getActivity<AppCompatActivity>().resources?.getString(R.string.string_info_dialog_mother))
            }
        })
        tv_dailog_bottom_second_text?.setOnClickListener(View.OnClickListener {
            windowBottomDialog?.dismiss()
            if (isSex) {
                setSexData(getActivity<AppCompatActivity>().resources?.getString(R.string.string_info_dialog_female))
            } else {
                setRelationshipData(getActivity<AppCompatActivity>().resources?.getString(R.string.string_info_dialog_father))
            }
        })
        tv_dailog_bottom_cancel_text?.setOnClickListener(View.OnClickListener {
            windowBottomDialog?.dismiss()
        })
    }


    /**
     * 宝贝姓名校验和获取
     */
     fun nameCheck():String {
        val et_apui_name = getView<ContainsEmojiEditText>(R.id.et_apui_name)
        val trim = et_apui_name.text.toString().trim()
        if (!TextUtils.isEmpty(trim)){
            return trim
        }else{
            LKToastUtil.showToastShort(getActivity<AppCompatActivity>().resources.getString(R.string.string_info_hint_name))
            return ""
        }
    }
    /**
     * 性别校验和获取
     */
    fun sexCheck():String {
        val tv_apui_sex = getView<TextView>(R.id.tv_apui_sex)
        val trim = tv_apui_sex.text.toString().trim()

        if (!TextUtils.isEmpty(trim)){
            return trim
        }else{
            LKToastUtil.showToastShort(getActivity<AppCompatActivity>().resources.getString(R.string.string_info_toast_sex))
            return ""
        }
    }
    /**
     * 性别
     */
     fun getsex():Int {
        val tv_apui_sex = getView<TextView>(R.id.tv_apui_sex)
        val trim = tv_apui_sex.text.toString().trim()
        if (TextUtils.equals(trim,getActivity<AppCompatActivity>().resources?.getString(R.string.string_info_dialog_male))){
              return 1
        }else if (TextUtils.equals(trim,getActivity<AppCompatActivity>().resources?.getString(R.string.string_info_dialog_female))){
            return 0
        }
        return 0
    }
    /**
     *出生日期校验和获取
     */
     fun birthCheck():String {
        val tv_apui_birth_date = getView<TextView>(R.id.tv_apui_birth_date)
        val trim = tv_apui_birth_date.text.toString().trim()
        if (!TextUtils.isEmpty(trim)){
            return trim
        }else{
            LKToastUtil.showToastShort(getActivity<AppCompatActivity>().resources.getString(R.string.string_info_toast_birth_date))
            return ""
        }
    }
    /**
     * 亲子关系校验和获取
     */
     fun relationCheck():String {
        val tv_apui_relationship = getView<TextView>(R.id.tv_apui_relationship)
        val trim = tv_apui_relationship.text.toString().trim()
        if (!TextUtils.isEmpty(trim)){
            return trim
        }else{
            LKToastUtil.showToastShort(getActivity<AppCompatActivity>().resources.getString(R.string.string_info_toast_relationship_left))
            return ""
        }
    }
    /**
     * 真实姓名校验和获取
     */
     fun myNameionCheck():String {
        val tv_apui_relationship = getView<ContainsEmojiEditText>(R.id.et_apui_my_name)
        val trim = tv_apui_relationship.text.toString().trim()
        if (!TextUtils.isEmpty(trim)){
            return trim
        }else{
            LKToastUtil.showToastShort(getActivity<AppCompatActivity>().resources.getString(R.string.string_info_hint_may_name))
            return ""
        }
    }
    /**
     * 设置性别数据
     */
    private fun setSexData(content: String?) {
        val tv_apui_sex = getView<TextView>(R.id.tv_apui_sex)
        if (!TextUtils.isEmpty(content)) {
            tv_apui_sex.text = content
        }
    }

    /**
     * 设置亲子关系数据
     */
    private fun setRelationshipData(content: String?) {
        val tv_apui_relationship = getView<TextView>(R.id.tv_apui_relationship)
        if (!TextUtils.isEmpty(content)) {
            tv_apui_relationship.text = content
        }
    }

    /**
     * 设置时间数据
     */
    fun setTimeData(content: String?) {
        val tv_apui_birth_date = getView<TextView>(R.id.tv_apui_birth_date)
        if (!TextUtils.isEmpty(content)) {
            tv_apui_birth_date.text = content
        }
    }
    /**
     * 设置标题栏
     */
    fun setTitle(title:String?){
        if (!TextUtils.isEmpty(title)){
            val jtb_apui= getView<SKTopBar>(R.id.jtb_apui)
            jtb_apui.setTitle(title)
        }
        setEvent()
    }

    /**
     * 设置设置事件
     */
    fun setEvent(){
        val jtb_apui= getView<SKTopBar>(R.id.jtb_apui)
        jtb_apui?.setEventInterface(object : SKTopBar.EventInterface{
            override fun leftOnClick() {
                getActivity<PerfectUserInfoActivity>().finish()

            }
            override fun rightOnClick() {
            }

        })
    }

}